package com.buer.storage.service.impl;

import com.buer.storage.entity.Dorder;
import com.buer.storage.entity.DorderDO;
import com.buer.storage.entity.DorderdetailsDO;
import com.buer.storage.entity.Stock;
import com.buer.storage.otherEntity.Formmain67DO;
import com.buer.storage.otherEntity.FormmainDO;
import com.buer.storage.otherEntity.Formson126DO;
import com.buer.storage.otherEntity.FormsonDO;
import com.buer.storage.otherService.Formmain67Service;
import com.buer.storage.otherService.FormmainService;
import com.buer.storage.otherService.Formson126Service;
import com.buer.storage.otherService.FormsonService;
import com.buer.storage.service.*;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.utils.CommonGeneratorUtil;
import com.buer.storage.vo.StockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2021/1/27.
 */
@Service
public class OaSynchronizationServiceImpl implements OaSynchronizationService {

    @Autowired
    private FormmainService formmainService;

    @Autowired
    private FormsonService formsonService;

    @Autowired
    private StockService stockService;

    @Autowired
    private StockdetailsService stockdetailsService;

    @Autowired
    private Formmain67Service formmain67Service;

    @Autowired
    private Formson126Service formson126Service;

    @Autowired
    private DorderService dorderService;

    @Autowired
    private DorderdetailsService dorderdetailsService;

    public static Map<Integer, Integer> stockMap = new HashMap<>();

    @Override
    @Transactional
    public void synchronizationOa() {
        //删除存货台账formson_0083（明细表）数据
        //formsonService.removeAllByFormmainId()

        //formmainService.removeAll();
        //删除存货台账formmain_0064（主表）数据
        Map<String, Object> formmainMap = new HashMap<>();
        formmainMap.put("field0003", "战勤保障仓库");
        List<FormmainDO> formmainList = formmainService.getFormmainList(formmainMap);
        for (FormmainDO formmainDO : formmainList) {
            //1、删除存货台账formson_0083（明细表）数据
            formsonService.removeAllByFormmainId(formmainDO.getID());
            //2、删除存货台账formmain_0064（主表）数据
            formmainService.remove(formmainDO.getID());
        }


        //查询Stock库存表，循环插入formmain_0064
        List<StockVo> list = stockService.getAllStock();

        if (list != null && list.size() > 0) {
            for (StockVo stock : list) {
                //存入缓存
                stockMap.put(stock.getStockId(), stock.getProdResiduenum());
                //formmain_0064相当于库存表
                //战勤保障仓库
                //-1431540310537453399,-6371938368401290244,-3896768608988869526,8654353675032481110,-634968230286048584,3112355376323838988,8281270193816617297,113700441563088177,-5708518992074946596,9072462454743712695,-5554168845774864341,-1372475341319682736,3106218472258552742,-7888461977666566902,4838136687474460014,-1562677343533904240,5185180553399782695,-2579610099728102805
                FormmainDO formmainDO = new FormmainDO();
                //ID
                Long aLong = Long.valueOf(stock.getStockId());
                formmainDO.setID(aLong);
                //存货编码--改为库存ID
                formmainDO.setField0004(stock.getStockId() + "");
                //存货分类
                formmainDO.setField0005(stock.getCategoryTwoName());
                //单位
                formmainDO.setField0022(stock.getEmit());
                //剩余数量
                formmainDO.setField0006(new BigDecimal(stock.getProdResiduenum()));
                formmainDO.setField0007(new BigDecimal(stock.getProdResiduenum()));
                formmainDO.setField0008(new BigDecimal(0));
                //装备名称
                formmainDO.setField0033(stock.getProdName());
                //仓库名称
                formmainDO.setField0003("战勤保障仓库");
                //权限部门
                formmainDO.setField0043("-1431540310537453399,-6371938368401290244,-3896768608988869526,8654353675032481110,-634968230286048584,3112355376323838988,8281270193816617297,113700441563088177,-5708518992074946596,9072462454743712695,-5554168845774864341,-1372475341319682736,3106218472258552742,-7888461977666566902,4838136687474460014,-1562677343533904240,5185180553399782695,-2579610099728102805,5830603372400820928,-8497310025554899320,90396279497443717,-9217979048687808866,3554928017844421791,-9045156341883712171,189120679617906235,-9087002256099424476,-5056830854887291702,-1209121220649071108,-5515728760082116942,-2490116394630165552,1370380090569081773,4143356451652585159");
                formmainService.save(formmainDO);

                //插入formson_0083
                FormsonDO formsonDO = new FormsonDO();
                //主键id
                formsonDO.setID(aLong);
                //formmain_0064 id
                formsonDO.setFormmainId(aLong);
                //规格型号
                formsonDO.setField0012(stock.getProdSpecial());
                //明细表剩余数量
                formsonDO.setField0013(new BigDecimal(stock.getProdResiduenum()));
                formsonDO.setField0016(new BigDecimal(stock.getProdResiduenum()));
                //存放地点
                // formsonDO.setField0026(stockDetail.getPosition());

                //放入批次
                formsonDO.setField0019("CHYSR" + aLong);

                formsonService.save(formsonDO);


            }
        }
    }

    @Override
    public void synchronizationPartOa() {
        if (CheckEmptyUtil.isNotEmpty(stockMap)) {
            List<StockVo> updateList = new ArrayList<>();
            //获得所有库存
            List<StockVo> list = stockService.getAllStock();
            //循环比对
            for (StockVo stockVo : list) {
                Integer num = stockMap.get(stockVo.getStockId());
                if (num == null || num != stockVo.getProdResiduenum()) {
                    updateList.add(stockVo);
                }
            }

            if (CheckEmptyUtil.isNotEmpty(updateList)) {

                //有数据不同，更新sqlserver
                for (StockVo upStock : updateList) {
                    FormmainDO formmainDO = formmainService.get(Long.valueOf(upStock.getStockId()));

                    formmainDO.setField0006(new BigDecimal(upStock.getProdResiduenum()));
                    formmainDO.setField0007(new BigDecimal(upStock.getProdResiduenum()));
                    formmainService.update(formmainDO);

                    FormsonDO formsonDO = formsonService.get(Long.valueOf(upStock.getStockId()));
                    formsonDO.setField0013(new BigDecimal(upStock.getProdResiduenum()));
                    formsonDO.setField0016(new BigDecimal(upStock.getProdResiduenum()));
                    formsonService.update(formsonDO);

                }
            }
        }
    }

    @Override
    @Transactional
    public void deliverySynchronization() {
        Map<String, Object> map = new HashMap<>();
        map.put("finishedflag", 1);
        //查询近2天的
        map.put("days", 2);

        List<Formmain67DO> formson67List = formmain67Service.getFormmain67List(map);
        for (Formmain67DO formmain67DO : formson67List) {
            Map<String, Object> formsonMap = new HashMap<>();
            formsonMap.put("formmainId", formmain67DO.getId());
            List<Formson126DO> formson126List = formson126Service.getFormson126List(formsonMap);
            if (CheckEmptyUtil.isNotEmpty(formson126List)) {
                //查询出库订单表是否已存在
                Map<String, Object> dorderMap = new HashMap<>();
                dorderMap.put("platformType", 1);
                dorderMap.put("platformId", formmain67DO.getId());
                Dorder one = dorderService.getOne(dorderMap);
                if (one == null) {
                    //数据库不含有，存库
                    DorderDO dorder = new DorderDO();
                    dorder.setDorderNum(CommonGeneratorUtil.generatorDorderOrderNum());
                    dorder.setOutTypeid(1);
                    dorder.setOutStatus(2);
                    dorder.setOutDate(new Date());
                    dorder.setPlatformType(1);
                    dorder.setPlatformId(formmain67DO.getId());
                    dorderService.save(dorder);
                    //保存出库详情
                    Map<String, Object> stockMap = new HashMap<>();
                    int num=0;
                    for (Formson126DO formson126DO : formson126List) {
                        stockMap.put("stockId", formson126DO.getField0005());
                        Stock stock = stockService.getOne(stockMap);
                        if (stock != null) {
                            num=num+1;
                            //stock.setProdStocknum(stock.getProdStocknum() - formson126DO.getField0016());
                            stock.setProdAwaitnum(stock.getProdAwaitnum() + formson126DO.getField0016());
                            stock.setProdResiduenum(stock.getProdStocknum() - stock.getProdAwaitnum());
                            int update = stockService.update(stock);
                            if (update > 0) {
                                DorderdetailsDO dorderdetailsDO = new DorderdetailsDO();
                                dorderdetailsDO.setProdName(stock.getProdName());
                                dorderdetailsDO.setProdImg(stock.getProdImg());
                                dorderdetailsDO.setProdCode(stock.getProdCode());
                                dorderdetailsDO.setProdResiduenum(stock.getProdResiduenum());
                                dorderdetailsDO.setProdSpecial(stock.getProdSpecial());
                                dorderdetailsDO.setProdNum(formson126DO.getField0016());
                                dorderdetailsDO.setEmit(stock.getEmit());
                                dorderdetailsDO.setProdArea(stock.getProdArea());
                                dorderdetailsDO.setProdDepart(stock.getProdDepart());
                                dorderdetailsDO.setDorderOutid(dorder.getOutId());
                                dorderdetailsDO.setCategoryTwoId(stock.getCategoryTwoId());
                                dorderdetailsDO.setStockId(stock.getStockId());
                                dorderdetailsService.save(dorderdetailsDO);
                            }

                        }

                    }

                    if(num==0){
                        stockService.remove(dorder.getOutId());
                    }

                }
            }
        }

    }
}
